package com.jcrj.service;

import com.jcrj.dao.UserDao;
import com.jcrj.entity.ORole;
import com.jcrj.entity.ORoleUser;
import com.jcrj.entity.OUser;
import com.jcrj.util.DiscuzUtil;
import com.jcrj.util.GetText;
import com.jcrj.util.MD5;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

public class UserService {
	private UserDao userDao;

	public UserDao getUserDao() {
		return this.userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public OUser login(String name, String password) {
		if ((name != null) && (password != null) && (!"".equals(name.trim())) && (!"".equals(password.trim()))) {
			return this.userDao.getUser(name, MD5.toMD5(password));
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<ORole> findRoleByUser(int id) {
		String hql = "from ORole r where r.id in (select o.roleId from ORoleUser o where o.userId=" + id + ")";
		return this.userDao.findByHql(hql);
	}

	public List<OUser> getUserList(String account, String name, String orgName, String roleId, String volume, int currentPage, int accountSort, int orgSort) {
		String hql = "from OUser o where o.del='0' ";
		if ((account != null) && (!"".equals(account.trim()))) {
			hql = hql + " and o.account like '%" + account.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		if ((orgName != null) && (!"".equals(orgName.trim()))) {
			hql = hql + " and o.orgName like '%" + orgName.trim() + "%'";
		}
		if ((roleId != null) && (!"".equals(roleId.trim()))) {
			hql = hql + " and o.id in(select userId from ORoleUser where roleId=" + roleId + ")";
		}
		if ((volume != null) && (!"".equals(volume.trim()))) {
			hql = hql + " and o.volume>=" + volume.trim();
		}
		if ((accountSort == 0) && (orgSort == 0)) {
			hql = hql + " order by o.px asc";
		}
		if (accountSort != 0) {
			if (accountSort % 2 == 1)
				hql = hql + " order by o.account asc";
			else {
				hql = hql + " order by o.account desc";
			}
		}
		if (orgSort != 0) {
			if (orgSort % 2 == 1)
				hql = hql + " order by o.orgName asc";
			else {
				hql = hql + " order by o.orgName desc";
			}
		}
		List<OUser> list = this.userDao.findByPage(hql, currentPage);
		for (OUser user : list) {
			user.setOrgName(getUserAllOrgName(user.getOrgId().intValue()));
		}
		return list;
	}

	public List<OUser> deleteUserList(String account, String name, String orgName, String roleId, String volume, int currentPage, int accountSort, int orgSort) {
		String hql = "from OUser o where o.del='1' ";
		if ((account != null) && (!"".equals(account.trim()))) {
			hql = hql + " and o.account like '%" + account.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		if ((orgName != null) && (!"".equals(orgName.trim()))) {
			hql = hql + " and o.orgName like '%" + orgName.trim() + "%'";
		}
		if ((roleId != null) && (!"".equals(roleId.trim()))) {
			hql = hql + " and o.id in(select userId from ORoleUser where roleId=" + roleId + ")";
		}
		if ((volume != null) && (!"".equals(volume.trim()))) {
			hql = hql + " and o.volume>=" + volume.trim();
		}
		if ((accountSort == 0) && (orgSort == 0)) {
			hql = hql + " order by o.px asc";
		}
		if (accountSort != 0) {
			if (accountSort % 2 == 1)
				hql = hql + " order by o.account asc";
			else {
				hql = hql + " order by o.account desc";
			}
		}
		if (orgSort != 0) {
			if (orgSort % 2 == 1)
				hql = hql + " order by o.orgName asc";
			else {
				hql = hql + " order by o.orgName desc";
			}
		}
		List<OUser> list = this.userDao.findByPage(hql, currentPage);
		for (OUser user : list) {
			user.setOrgName(getUserAllOrgName(user.getOrgId().intValue()));
		}
		return list;
	}

	public String getUserAllOrgName(int orgId) {
		String sql = "select type,name from o_org start with id=" + orgId + "  connect by prior pid=id order by id desc";
		List<Object[]> list = this.userDao.findBySql(sql);
		String orgName = "";
		for (Object[] objs : list) {
			if (orgName.equals(""))
				orgName = objs[1].toString();
			else {
				orgName = objs[1].toString() + "." + orgName;
			}
			if (objs[0].equals("1")) {
				break;
			}
		}
		return orgName;
	}

	public Long getUserCount(String account, String name, String orgName, String roleId, String volume) {
		String hql = "select count(id) from OUser o where o.del='0' ";
		if ((account != null) && (!"".equals(account.trim()))) {
			hql = hql + " and o.account like '%" + account.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		if ((orgName != null) && (!"".equals(orgName.trim()))) {
			hql = hql + " and o.orgName like '%" + orgName.trim() + "%'";
		}
		if ((roleId != null) && (!"".equals(roleId.trim()))) {
			hql = hql + " and o.id in(select userId from ORoleUser where roleId=" + roleId + ")";
		}
		if ((volume != null) && (!"".equals(volume.trim()))) {
			hql = hql + " and o.volume>=" + volume.trim();
		}
		return this.userDao.countTotalByHql(hql);
	}

	public Long deleteUserCount(String account, String name, String orgName, String roleId, String volume) {
		String hql = "select count(id) from OUser o where o.del='1' ";
		if ((account != null) && (!"".equals(account.trim()))) {
			hql = hql + " and o.account like '%" + account.trim() + "%'";
		}
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
		}
		if ((orgName != null) && (!"".equals(orgName.trim()))) {
			hql = hql + " and o.orgName like '%" + orgName.trim() + "%'";
		}
		if ((roleId != null) && (!"".equals(roleId.trim()))) {
			hql = hql + " and o.id in(select userId from ORoleUser where roleId=" + roleId + ")";
		}
		if ((volume != null) && (!"".equals(volume.trim()))) {
			hql = hql + " and o.volume>=" + volume.trim();
		}
		return this.userDao.countTotalByHql(hql);
	}

	public boolean viliadeAccount(String account) {
		boolean flag = false;
		String hql = "from OUser o where o.account='" + account + "'";
		List list = this.userDao.findByHql(hql);
		if ((list != null) && (!list.isEmpty())) {
			flag = true;
		}
		return flag;
	}

	public void saveUser(OUser user) {
		user.setPsw(MD5.toMD5(user.getPsw()));
		this.userDao.saveUser(user);
	}

	public void saveUser(OUser user, String roleId) {
		user.setPsw(MD5.toMD5(user.getPsw()));
		this.userDao.saveUser(user);
		if ((roleId != null) && (!"".equals(roleId))) {
			String[] ids = roleId.split(",");
			for (int i = 0; i < ids.length; i++) {
				ORoleUser roleUser = new ORoleUser();
				roleUser.setRoleId(Integer.valueOf(Integer.parseInt(ids[i])));
				roleUser.setUserId(user.getId());
				this.userDao.saveUserRole(roleUser);
			}
		}
		DiscuzUtil dis = new DiscuzUtil();
		dis.addUser(user.getAccount());
	}

	public void updateUser(OUser user) {
		this.userDao.updateUser(user);
	}

	public void updateUser(OUser user, String roleId) {
		this.userDao.updateUser(user);
		String hql = "delete from ORoleUser o where o.userId=" + user.getId();
		this.userDao.executeHql(hql);
		if ((roleId != null) && (!"".equals(roleId))) {
			String[] ids = roleId.split(",");
			for (int i = 0; i < ids.length; i++) {
				ORoleUser roleUser = new ORoleUser();
				roleUser.setRoleId(Integer.valueOf(Integer.parseInt(ids[i])));
				roleUser.setUserId(user.getId());
				this.userDao.saveUserRole(roleUser);
			}
		}
	}

	public OUser getUserById(int id) {
		return (OUser) this.userDao.getObject(new OUser(), id);
	}

	public String getNamesById(String users) {
		String names = "";
		String hql = "from OUser where id in(" + users + ")";
		List<OUser> list = this.userDao.findByHql(hql);
		if ((list != null) && (!list.isEmpty())) {
			for (OUser user : list) {
				if ("".equals(names))
					names = user.getName();
				else {
					names = names + "," + user.getName();
				}
			}
		}
		return names;
	}

	public void deleteUser(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "update OUser set del='1' where id in(" + id + ")";
			this.userDao.executeHql(hql);
		}
	}

	public void deleteAllUser(String[] ids) {
		if (ids != null)
			for (int i = 0; i < ids.length; i++) {
				OUser user = getUserById(Integer.parseInt(ids[i]));
				this.userDao.deleteUser(user);
				String hql1 = "delete from ORoleUser where userId =" + ids[i];
				this.userDao.executeHql(hql1);
				DiscuzUtil dis = new DiscuzUtil();
				dis.deleteDizUser(user.getAccount());
			}
	}

	public void updateRestoreUser(String[] ids) {
		if (ids != null) {
			String id = "";
			for (int i = 0; i < ids.length; i++) {
				if (id == "")
					id = id + ids[i];
				else {
					id = id + "," + ids[i];
				}
			}
			String hql = "update OUser set del='0' where id in(" + id + ")";
			this.userDao.executeHql(hql);
		}
	}

	public List<OUser> userRolesForPage(int roleId, String account, String name, int currentPage) {
		String hql = "from OUser o where o.del='0' and o.id in (select userId from ORoleUser where roleId=" + roleId + ")";
		if ((account != null) && (!account.equals(""))) {
			hql = hql + " and o.account like '%" + account + "%'";
		}
		if ((name != null) && (!name.equals(""))) {
			hql = hql + " and o.name like '%" + name + "%'";
		}
		hql = hql + " order by o.px asc";
		List<OUser> list = this.userDao.findByPage(hql, currentPage);
		for (OUser u : list) {
			u.setOrgName(getUserAllOrgName(u.getOrgId().intValue()));
		}
		return list;
	}

	public long userRolesForCount(int roleId, String account, String name) {
		String hql = "select count(o.id) from OUser o where o.del='0' and o.id in (select userId from ORoleUser where roleId=" + roleId + ")";
		if ((account != null) && (!account.equals(""))) {
			hql = hql + " and o.account like '%" + account + "%'";
		}
		if ((name != null) && (!name.equals(""))) {
			hql = hql + " and o.name like '%" + name + "%'";
		}
		Long total = this.userDao.countTotalByHql(hql);
		return total.longValue();
	}

	public List<OUser> userNoCurRolesForPage(int roleId, String account, String name, int currentPage) {
		String hql = "select o.id,o.account,o.name,o.phone,o.org_id from O_User o where o.del='0' and o.id not in(select user_id from O_Role_User  where role_Id ="
				+ roleId + ")";
		if ((account != null) && (!account.equals(""))) {
			hql = hql + " and o.account like '%" + account + "%'";
		}
		if ((name != null) && (!name.equals(""))) {
			hql = hql + " and o.name like '%" + name + "%'";
		}
		hql = hql + " order by o.px asc";
		List<Object[]> list = this.userDao.findByPageSql(hql, currentPage);
		List userList = new ArrayList();
		if ((list != null) && (list.size() > 0)) {
			for (Object[] obj : list) {
				OUser u = new OUser();
				u.setId(Integer.valueOf(new BigDecimal(obj[0].toString()).intValue()));
				u.setAccount(obj[1].toString());
				u.setName(obj[2].toString());
				u.setPhone(GetText.isEmpty(obj[3]));
				u.setOrgName(getUserAllOrgName(new BigDecimal(obj[4].toString()).intValue()));
				userList.add(u);
			}
		}
		return userList;
	}

	public Long userNoCurRolesForCount(int roleId, String account, String name) {
		String hql = "select count(o.id) from OUser o where o.del='0' and o.id not in(select userId from ORoleUser where roleId =" + roleId + ")";
		if ((account != null) && (!account.equals(""))) {
			hql = hql + " and o.account like '%" + account + "%'";
		}
		if ((name != null) && (!name.equals(""))) {
			hql = hql + " and o.name like '%" + name + "%'";
		}
		Long total = this.userDao.countTotalByHql(hql);
		return total;
	}

	public void saveRoleUser(int roleId, String[] ids) {
		if ((ids != null) && (ids.length > 0))
			for (int i = 0; i < ids.length; i++) {
				ORoleUser ou = new ORoleUser();
				ou.setRoleId(Integer.valueOf(roleId));
				ou.setUserId(Integer.valueOf(Integer.parseInt(ids[i])));
				this.userDao.saveUserRole(ou);
			}
	}

	public void deleteRoleUser(int roleId, String[] ids) {
		if ((ids != null) && (ids.length > 0))
			for (int i = 0; i < ids.length; i++) {
				String hql = "delete from ORoleUser where roleId=" + roleId + " and  userId=" + ids[i];
				this.userDao.executeHql(hql);
			}
	}

	public List<OUser> getUserOrg(int orgId) {
		String hql = "from OUser o where o.del='0' and o.orgId=" + orgId;
		return this.userDao.findByHql(hql);
	}

	public List<OUser> searchUserList(String name) {
		String hql = "from OUser o where o.del='0' ";
		if ((name != null) && (!"".equals(name.trim()))) {
			hql = hql + " and o.name like '%" + name.trim() + "%'";
			hql = hql + " or o.account like '%" + name.trim() + "%' and o.del='0'";
		}
		hql = hql + " order by o.px asc";
		return this.userDao.findByHql(hql);
	}

	public boolean hasPermission(String userId, String sessionUser) {
		boolean flag = false;
		boolean permission = false;
		if ((userId != null) && (!"".equals(userId))) {
			String curenthql = "from ORole where id in (select roleId from ORoleUser where userId =" + sessionUser + ")";
			List<ORole> sessionRoles = this.userDao.findByHql(curenthql);
			for (ORole role : sessionRoles) {
				if ("4".equals(role.getType())) {
					permission = true;
					return permission;
				}
			}
			if ((!permission) && (sessionUser.equals(userId)))
				flag = true;
		} else {
			flag = true;
		}
		return flag;
	}

	public OUser loginCert(String number) {
		String hql = " from OUser where number='" + number + "'";
		List list = this.userDao.findByHql(hql);
		if ((list != null) && (list.size() > 0)) {
			return (OUser) list.get(0);
		}
		return null;
	}
}

/*
 * Location: /Volumes/work/oa/WEB-INF/classes/ Qualified Name:
 * com.jcrj.service.UserService JD-Core Version: 0.6.0
 */